package com.ytd.common.util;

import com.ytd.common.bean.BaseRequest;
import com.ytd.common.bean.request.BatchAddOrderRequestData;
import com.ytd.common.net.NetCallBack;
import com.ytd.common.net.RequestManager;

import java.util.List;
import java.util.Random;

/**
 * 离线数据上传管理
 *
 * @author gaoguanchao
 * @since 2023/5/16
 */
public class LocalDataManager {

    private Random mRandom = new Random();
    private int total;
    private int uploadedCount;
    private int page = 0;
    private int pageSize = 5;

    private LocalDataCallBack mLocalDataCallBack;
    private boolean isStart;

    public boolean isStart() {
        return isStart;
    }

    public void setLocalDataCallBack(LocalDataCallBack localDataCallBack) {
        mLocalDataCallBack = localDataCallBack;
    }

    public void startUpload() {
        if (mLocalDataCallBack == null) {
            return;
        }
        total = mLocalDataCallBack.getCount();
        uploadedCount = mLocalDataCallBack.getUploadedCount();
        page = uploadedCount / pageSize;
        LogUtil.d(
            "uploadLocalOrder startUpload uploadedCount:" + uploadedCount + "---total:" + total + "---page:" + page);
        if (total == 0) {
            sendEmptyRequest();
            return;
        }
        isStart = true;
        int delayed = mRandom.nextInt(300);
        LogUtil.d("uploadLocalOrder delayed:" + delayed);
        ThreadUtil.executeMainDelayed(mRunnable, delayed * 1000L);
    }

    Runnable mRunnable = this::uploadData;

    private void sendEmptyRequest() {
        BaseRequest request = new BaseRequest();
        BatchAddOrderRequestData requestData = new BatchAddOrderRequestData();
        requestData.totalCount = String.valueOf(0);
        requestData.uploadedCount = String.valueOf(0);
        request.data = requestData;
        request.interfaceName = mLocalDataCallBack.getInterfaceName();
        RequestManager.getInstance().sendRequest(request);
    }

    private void uploadData() {
        int start = page * pageSize;
        if (start > total) {
            LogUtil.d("uploadData endUpload start > total");
            endUpload();
            return;
        }
        List data = mLocalDataCallBack.getData(start, pageSize);
        if (data == null || data.size() == 0) {
            LogUtil.d("uploadData endUpload data == null || data.size() == 0");
            endUpload();
            return;
        }
        BaseRequest request = new BaseRequest();
        BatchAddOrderRequestData requestData = new BatchAddOrderRequestData();
        requestData.list = data;
        requestData.totalCount = String.valueOf(total);
        requestData.uploadedCount = String.valueOf(uploadedCount);
        request.data = requestData;
        request.interfaceName = mLocalDataCallBack.getInterfaceName();
        RequestManager.getInstance().request(request, new NetCallBack<Object>() {
            @Override
            public void onSuccess(Object result) {
                page++;
                uploadedCount += data.size();
                for (Object datum : data) {
                    mLocalDataCallBack.update(datum);
                }
                LogUtil.d("uploadLocalOrder onSuccess uploadedCount:" + uploadedCount + "---total:" + total);
                if (uploadedCount == total) {
                    endUpload();
                    return;
                }
                uploadData();
            }

            @Override
            public void onFail(int code, String msg) {
                LogUtil.d("uploadLocalOrder onFail " + code + "---" + msg);
                endUpload();
            }
        });
    }

    private void endUpload() {
        isStart = false;
        LogUtil.d(
            "uploadLocalOrder endUpload uploadedCount:" + uploadedCount + "---total:" + total + "---page:" + page);
        if (total != 0 && total == uploadedCount) {
            LogUtil.d(
                "uploadLocalOrder deleteUploadData");
            mLocalDataCallBack.deleteUploadData();
        }
        page = 0;
        total = 0;
        uploadedCount = 0;
    }

    private LocalDataManager() {
    }

    public static LocalDataManager getInstance() {
        return ManagerHolder.instance;
    }

    public void uploadNow() {
        if (mLocalDataCallBack == null) {
            return;
        }
        if (isStart) {
            ThreadUtil.removeMainRunnable(mRunnable);
        }
        total = mLocalDataCallBack.getCount();
        uploadedCount = mLocalDataCallBack.getUploadedCount();
        page = uploadedCount / pageSize;
        LogUtil.d(
            "uploadLocalOrder startUpload uploadedCount:" + uploadedCount + "---total:" + total + "---page:" + page);
        if (total == 0) {
            sendEmptyRequest();
            return;
        }
        isStart = true;
        ThreadUtil.executeMain(mRunnable);
    }

    private static class ManagerHolder {
        private static LocalDataManager instance = new LocalDataManager();
    }

    public interface LocalDataCallBack<T> {
        List<T> getData(int start, int pageSize);

        int getCount();

        int getUploadedCount();

        String getInterfaceName();

        void update(T t);

        void deleteUploadData();
    }
}
